<template>
  <div>
    <router-link to="/survey/leader">
      <el-button>首页</el-button>
    </router-link>
    <div class="login_box">
      <b style="color: #909399; font-size: 30px;">{{ power }}</b>
      <!--输入框  :model="loginForm"绑定数据-->
      <el-form ref="loginRef" label-width="0px" :model="loginForm" class="login_form" :rules='loginFormRules'>
        <el-form-item prop="username">
          <!--账号-->
          <el-input prefix-icon="el-icon-user-solid" placeholder="请输入账号" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!--密码 prop绑定验证-->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" v-model="loginForm.password"
                    show-password></el-input>
        </el-form-item>
        <!--登录-->
        <el-form-item class="btn">
          <el-button type="primary" round @click="login"> 登 录</el-button>
          <el-link icon="el-icon-s-tools" target="_blank" type="info"
                   :underline="false">
            <router-link to="/sign">还没有账号?去注册</router-link>
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      power: '用户登录',
      activeIndex: '1',
      activeIndex2: '1',
      loginForm: {
        username: '',
        password: ''
      },
      //表单的验证规则对象
      loginFormRules: {
        username: [
          {required: true, message: '请输入登录名称!', trigger: 'blur'},
          {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码!', trigger: 'blur'},
          {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    login() {
      //表单的校验
      this.$refs.loginRef.validate((valid) => {
        console.log(valid)
        if (!valid) return;
        //发送post请求
        this.$http.post('/user/login', this.loginForm).then(resp => {
          console.log(resp)
          if (resp.flag == true) {
            console.log(resp.data.token)
            //将token保存到SessionStorage
            window.sessionStorage.setItem('token', resp.data.token)
            window.sessionStorage.setItem('userId', resp.data.userId)
            window.sessionStorage.setItem('name', resp.data.name)
            this.$message.success("登录成功!")
            this.$store.commit('insertUserId', resp.data.userId);
            this.$store.commit('updateName', resp.data.name);

            console.log("returnPath" + this.$route.params.returnPath);
            if (this.$route.params.returnPath == undefined) {
              console.log("是否为undefined" + this.$store.state.name);
              this.$router.push({name: 'leader'})
            } else {
              window.open(this.$route.params.returnPath)
            }
          } else {
            this.$alert('登录失败,账号或密码错误!', '提示:', {
              confirmButtonText: '确定',
              callback: action => {
                this.$message({
                  type: 'error',
                  message: '登录失败!'
                });
              }
            });
          }
          console.log(resp.data);
        });
      });
    }
  }
}
</script>
<style lang="less" scoped>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}


.login_box {
  width: 20%;
  min-width: 350px;
  height: 30%;
  min-height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 40%;
  /*屏幕正中间*/
  transform: translate(-50%, -50%);
  z-index: 99;
}

.btn {
  display: flex;
  /*尾部对齐*/
  justify-content: flex-end;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 40px;
  box-sizing: border-box;
}

.login_box b {
  position: relative;
  top: 10%;
  left: 10%;
}
</style>